<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="供客名称" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供客名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供客负责人" prop="supplyContact">
        <el-input
          v-model="queryParams.supplyContact"
          placeholder="请输入供客负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
            <el-form-item label="供客类型" prop="isType">
              <el-select v-model="queryParams.isType" placeholder="请选择供客类型" @keyup.enter.native="handleQuery">
                <el-option
                  v-for="dict in dict.type.supermarket_supply"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supermarket:supply:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplyList">
      <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
      <el-table-column label="供客编号" align="center" prop="supplyCode" width="180"/>
      <el-table-column label="供客名称" align="center" prop="supplyName" width="180"/>
      <el-table-column label="供客负责人" align="center" prop="supplyContact" width="100"/>
      <el-table-column label="供客电话" align="center" prop="supplyPhone" width="120"/>
      <el-table-column label="供客地址" align="center" prop="supplierAddress" width="250"/>
      <el-table-column
        label="供客类型"
        align="center"
        prop="isType"
        width="150"
      >
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.isType === '1'">供应商</el-tag>
          <el-tag type="success" v-if="scope.row.isType === '2'">客户</el-tag>
        </template>
      </el-table-column>
       <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="150"/>
      <el-table-column label="创建人" align="center" prop="createName" width="100"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"
        fixed="right"
      width="180"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supermarket:supply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row,scope.$index)"
            v-hasPermi="['supermarket:supply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供客对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="供客名称" prop="supplyName">
          <el-input v-model="form.supplyName" placeholder="请输入供客名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供客负责人" prop="supplyContact">
          <el-input v-model="form.supplyContact" placeholder="请输入供客负责人" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供客电话" prop="supplyPhone">
          <el-input v-model="form.supplyPhone" placeholder="请输入供客电话" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="供客地址" prop="supplierAddress">
          <el-input v-model="form.supplierAddress" placeholder="请输入供客地址" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="供客类型"  prop="isType">
              <el-select v-model="form.isType" placeholder="请选择供客类型">
                <el-option
                  v-for="dict in dict.type.supermarket_supply"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSupplyOne,getSupply, delSupply, addSupply, updateSupply } from "@/api/supermarket/supply";

export default {
  name: "Supply",
    dicts: ['sys_normal_disable','supermarket_supply'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供客表格数据
      supplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplyCode: null,
        supplyName: null,
        supplyContact: null,
        supplyPhone: null,
        isType: null,
        status: null,
        createName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplyName: [
          { required: true, message: "供客名称不能为空", trigger: "blur" }
        ],
        supplyContact: [
          { required: true, message: "供客负责人不能为空", trigger: "blur" }
        ],
        supplyPhone: [
          { required: true, message: "供客电话不能为空", trigger: "blur" }
        ],
        isType: [
          { required: true, message: "供客类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供客列表 */
    getList() {
      this.loading = true;
      const userName = this.$store.state.user.name
      this.queryParams.createName = userName;
      listSupplyOne(this.queryParams).then(response => {
        this.supplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplyId: null,
        supplyCode: null,
        supplyName: null,
        supplyContact: null,
        supplyPhone: null,
        supplierAddress: null,
        isType: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createName: null,
        updateName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供客";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplyId = row.supplyId || this.ids
      getSupply(supplyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供客";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supplyId != null) {
            updateSupply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      const i = index + 1
      const supplyIds = row.supplyId || this.ids;
      this.$modal.confirm('是否确认删除第' + i + '行的数据吗？').then(function() {
        return delSupply(supplyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supermarket/supply/export', {
        ...this.queryParams
      }, `supply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
